package com.fallsvc.re.javase;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Locale;

/**
 * @auther falls_vc
 * description:
 * @date 2025/10/11  8:10
 */
public class Demo2 {
//    冒泡排序
    public static void bubbleSort(int[] arr) {
        int len = arr.length;
        for (int i = 0;i < len - 1;i++) { // 1. 优化一 -1
            boolean flag = false;
            for (int j = 1; j < len - i;j++) {
                if (arr[j-1] > arr[j]) {
                    swap(arr,j-1,j);
                    flag = true;
                }
            }
            if (!flag) {
                return; // 优化二 已经排序好
            }
        }
    }

    private static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    public static void main(String[] args) {
        int[] arr = {9,8,6,5,3,1};
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));
//        Arrays.sort();
        String a = "aabb";
        StringBuilder stringBuilder = new StringBuilder();


    }
}
